import sys
sys.path.append("..")
from arff import *
from splitonefile import *
from classiferresult import *
import unittest

class testCrossMatrix(unittest.TestCase):
    """
    CrossMatrix of not detail file
    two cases
    classresult.txt
    """
    filename = "classresult.txt"
    
    def testTrainCase(self):
        """
        the train part
        """
        r = getCrossMatrix(self.filename,True)
        self.assertEqual(r['0']['0'],0)
        self.assertEqual(r['0']['3'],41)
        self.assertEqual(r['2']['3'],4)
        self.assertEqual(r['3']['0'],0)
        
    def testTestCase(self):
        """
        the test part
        """
        r = getCrossMatrix(self.filename,False)
        self.assertEqual(r['0']['0'],0)
        self.assertEqual(r['0']['3'],11)
        self.assertEqual(r['2']['3'],1)
        self.assertEqual(r['3']['0'],0)

    def testParamWrong(self):
        """
        how to test Exception?
        """

class testCrossMatrixDetail(unittest.TestCase):
    """
    analysis the detail by cross matrix
    only the test part ,no train part
    """
    def testTestCase(self):
        """
        test with a smaller dataset which is easy to count
        """
        cm = getCrossMatixDetail("teststaticdetail.txt")
        self.assertEqual(cm[0][0],0)
        self.assertEqual(cm[0][2],0)
        self.assertEqual(cm[1][1],8)
        self.assertEqual(cm[1][3],1)
        self.assertEqual(cm[2][1],0)
        self.assertEqual(cm[3][1],2)
        self.assertEqual(cm[3][3],8)

class testGlobalOnTrain(unittest.TestCase):
    """
    check the get global result on training set 
    """
    filename = "classresult.txt"
    
    def testall(self):
        """
        test the average result
        """
        r = getGlobalResultOnTrain(self.filename)
        self.assertEqual(r['all']['tp'],0.555)
        self.assertEqual(r['all']['fp'],0.555)
        self.assertEqual(r['all']['precision'],0.308)
        self.assertEqual(r['all']['recall'],0.555)
        self.assertEqual(r['all']['fm'],0.396)

        r = getGlobalResultOnTest(self.filename)
        self.assertEqual(r['all']['tp'],0.52)
        self.assertEqual(r['all']['fp'],0.52)
        self.assertEqual(r['all']['precision'],0.27)
        self.assertEqual(r['all']['recall'],0.52)
        self.assertEqual(r['all']['fm'],0.356)
        
    def testdetail(self):
        """
        test the result on one class
        """
        r = getGlobalResultOnTrain(self.filename)
        self.assertEqual(r['detail']['0']['tp'],0)
        self.assertEqual(r['detail']['1']['fp'],0)
        self.assertEqual(r['detail']['2']['precision'],0)
        self.assertEqual(r['detail']['3']['recall'],1)
        self.assertEqual(r['detail']['0']['fm'],0)

        r = getGlobalResultOnTest(self.filename)
        self.assertEqual(r['detail']['0']['tp'],0)
        self.assertEqual(r['detail']['1']['fp'],0)
        self.assertEqual(r['detail']['2']['precision'],0)
        self.assertEqual(r['detail']['3']['recall'],1)
        self.assertEqual(r['detail']['0']['fm'],0)
        
class testDetailResult(unittest.TestCase):
    def testdis(self):
        r = getDetailResult("detailresult.txt")
        self.assertEqual(r[1]['dis'][0],0.006)
        self.assertEqual(r[1]['dis'][1],0.058)
        self.assertEqual(r[1]['dis'][2],0.)
        self.assertEqual(r[1]['dis'][3],0.935)

        self.assertEqual(r[43]['dis'][0],0.086)
        self.assertEqual(r[43]['dis'][1],0.625)
        self.assertEqual(r[43]['dis'][2],0.)
        self.assertEqual(r[43]['dis'][3],0.289)

    def testwrite(self):
        """
        read & write & read
        """
        thefile = "detailresult.txt"
        tmpfile = "tmpdetailresult.txt"
        rdic = getDetailResult(thefile)
        writeDetailResult(rdic,tmpfile)
        rdic2 = getDetailResult(tmpfile)

        self.assertEqual(len(rdic.keys()),len(rdic2.keys()))
        for k in rdic.keys():
            self.assertEqual(rdic[k],rdic2[k])

if __name__ == "__main__":
    unittest.main()   
